package com.lpc.android.abc.base.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lpc.android.abc.base.widget.MProgressDialog;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lpc on 2016/11/16.
 */

public abstract class BaseFragment<P extends BasePresenter> extends Fragment {

    protected MProgressDialog mProgressDialog;
    protected View roootView;
    protected Unbinder mUnbinder;

    protected P mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        roootView = View.inflate(getContext(), setFragmentLayoutId(), null);
        mUnbinder = ButterKnife.bind(this, roootView);
        return roootView;
    }

    protected abstract int setFragmentLayoutId();

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (mPresenter != null) {
            mPresenter.onDestory();
            mPresenter = null;
        }

        if (mUnbinder == Unbinder.EMPTY) {
            mUnbinder.unbind();
            mUnbinder = null;
        }

        roootView = null;
    }

    public void hideLoadingDialog() {

        if (mProgressDialog != null) {
            mProgressDialog.hide();
        }

    }

    public void showLoadingDialog() {
        mProgressDialog = new MProgressDialog(getContext(), true);
        mProgressDialog.show();
    }
}
